package com.zyq.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyq.common.entity.AclPermission;
import com.zyq.common.entity.AclRole;
import com.zyq.common.entity.AclRolePermission;
import com.zyq.common.entity.AclUserRole;
import com.zyq.common.vo.Result;
import com.zyq.system.dao.AclPermissionDao;
import com.zyq.system.dao.AclRoleDao;
import com.zyq.system.dao.AclRolePermissionDao;
import com.zyq.system.dao.AclUserRoleDao;
import com.zyq.system.service.AclRoleService;
import com.zyq.system.vo.FenUserRoleVo;
import com.zyq.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AclRoleServiceImpl implements AclRoleService {
    @Autowired
    private AclRoleDao aclRoleDao;
    @Autowired
    private AclUserRoleDao aclUserRoleDao;
    @Autowired
    private AclPermissionDao aclPermissionDao;

    @Override
    public Result<Map<String, Object>> findRole(String userId) {
        Map<String, Object> map = new HashMap<>();
        // 1. 查询出来所有的用户角色
        List<AclRole> aclRoles = aclRoleDao.selectList(null);
        // 2. 通过用户id查询出来，用户所具有的的角色  （连表查询）
        List<AclRole> aclRoles1 = aclRoleDao.queryRoleByUserId(userId);
        // 前端需要的是角色的id就可以展示出来了 stream流进行转化
        List<String> RoleIds = aclRoles1.stream().map(item -> item.getId()).collect(Collectors.toList());
        // 3. 将全部角色和用户对应的角色方法哦HashMap里面,返回给前端显示
        map.put("allRole",aclRoles);
        map.put("userRoleIds",RoleIds);
        return new Result<>(2000,"角色查询成功",map);
    }

    @Override
    public Result<String> fenUserRole(FenUserRoleVo fenUserRoleVo) {
        // 1. 先将用户原先的数据库表里面的数据删除（用户id和角色id的连表）  批量删除
        UpdateWrapper<AclUserRole> aclUserRoleUpdateWrapper = new UpdateWrapper<>();
        aclUserRoleUpdateWrapper.eq("user_id",fenUserRoleVo.getUserId());
        int delete = aclUserRoleDao.delete(aclUserRoleUpdateWrapper);
        // 2. 然后再将，新的权限分配（用户id和角色id的关联）存放到中间表当中


        // 2.1 使用两种方法添加(因为给的方法没有批量添加的方法)  sql语句  使用foreach进行循环
        List<AclUserRole> aclUserRoles = new ArrayList<>();
        for (String roleId : fenUserRoleVo.getRoleIds()) {
            AclUserRole aclUserRole = new AclUserRole();
            aclUserRole.setUserId(fenUserRoleVo.getUserId());
            aclUserRole.setRoleId(roleId);
            aclUserRole.setIsDeleted(0);
            aclUserRole.setGmtCreate(LocalDateTime.now());
            aclUserRole.setGmtModified(LocalDateTime.now());

            aclUserRoles.add(aclUserRole);
        }
        Integer integer = aclUserRoleDao.BatchInsertUserRoleIds(aclUserRoles);

        // 2.2 第二种方法 去实现service,一两句话搞定了
        /*List<AclUserRole> aclUserRoles = new ArrayList<>();
        for (String roleId : fenUserRoleVo.getRoleIds()) {
            AclUserRole aclUserRole = new AclUserRole();
            aclUserRole.setUserId(fenUserRoleVo.getUserId());
            aclUserRole.setRoleId(roleId);
            aclUserRole.setIsDeleted(0);
            aclUserRole.setGmtCreate(LocalDateTime.now());
            aclUserRole.setGmtModified(LocalDateTime.now());

            aclUserRoles.add(aclUserRole);
        }
        boolean b = aclUserRoleService.saveBatch(aclUserRoles);*/

        // 3. 返回成功
        if(integer == fenUserRoleVo.getRoleIds().size()){
            return new Result<>(2000,"修改角色成功","成功");
        }
        /*if(b){
            return new Result<>(2000,"修改角色成功","成功");
        }*/
        return new Result<>(5000,"修改角色失败","失败");
    }

    @Override
    public Result<IPage<AclRole>> findRoleByPage(Integer currentPage, Integer pageSize, RoleVo roleVo) {
        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);
        IPage<AclRole> page = new Page<>(currentPage,pageSize);

        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        if(StringUtils.hasText(roleVo.getRoleName())){
            System.out.println("like");
            String roleName = roleVo.getRoleName();
            wrapper.like("role_name",roleName);
        }
        if(roleVo.getIsDeleted() != null){
            System.out.println("jin");
            wrapper.eq("is_deleted",roleVo.getIsDeleted());
        }



        IPage<AclRole> aclRoleIPage = aclRoleDao.selectPage(page, wrapper);
        System.out.println("aclRoleIPage = " + aclRoleIPage.getRecords());
        return new Result<>(2000,"分页查询角色成功",aclRoleIPage);
    }

}
